const CONFIG = require("../../config")
const knex = require('knex')(CONFIG.mysql);
module.exports = {
    /*
    如何获取值 get请求方法   获取req的query方法取值
               post请求方法  获取req的body方法取值
    */
    async getUserList(req, res) {
        //获取url值
        const { current, size } = req.query;
        //console.log("-------", current, size);
        let data;
        knex('user_tab').select(
            "id",
            "isadmin",
            "username",
            "password",
            "telephone",
            "nickName",
            "avatarUrl",
            "friends_ids",
            "userlikeNum",
            "like_user_ids",
            "like_friend_ids",
            "like_community_ids",
            "isadmin"
        ).then(resp => {
            //密码加密

            data = resp;
        }).then(() => {
            if (data.length > 0) {
                items = {
                    code: 200,
                    data: {
                        current: current,
                        hitCount: false,
                        orders: [],
                        pages: 1,
                        records: data,
                        searchCount: true,
                        size: size,
                        total: 30
                    },
                    msg: "操作成功",
                    success: true
                }
                res.send(items)
            } else {
                res.send("error");
            }
        })
    },


    async postUser(req, res) {
        let { data, name } = req.body;

        knex('user_tab').insert({ data: data, name: name }).then(resp => {
                console.log("res", resp);
                if (resp) {
                    res.send({
                        code: 200,
                        msg: "操作成功",
                        success: true
                    })
                } else {
                    res.send({
                        code: 4001,
                        msg: "操作失败",
                        success: true
                    })
                }
            })
            //res.send('postUpdate');
    },

    async postUpdateUser(req, res) {
        let { username, password, id, avatarUrl } = req.body
        console.log("---------------", req.body)

        knex('user_tab').update({
            username: username,
            password: password,
            avatarUrl: avatarUrl
        }).where({ id: id }).then(resp => {
            console.log(resp);
            if (resp === 1) {
                res.send({
                    code: 200,
                    msg: "操作成功",
                    success: true
                })
            } else {
                res.send({
                    code: 4001,
                    msg: "操作失败",
                    success: true
                })
            }
        })
    },

    async postRemoveUser(req, res) {
        console.log("res._parsedUrl", req.query.id);
        const id = req.query.id;

        knex('user_tab').delete().where({ id: id }).then(resp => {
            console.log(resp);
            if (resp) {
                res.send({
                    code: 200,
                    msg: "操作成功",
                    success: true
                })
            } else {
                res.send({
                    code: 4001,
                    msg: "操作失败",
                    success: true
                })
            }
        })
    },

}